package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.qo.OrderChartQueryObject;
import cn.wolfcode.wms.qo.ProductStockQueryObject;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.qo.SaleChartQueryObject;
import cn.wolfcode.wms.service.*;
import cn.wolfcode.wms.util.Dictionary;
import cn.wolfcode.wms.util.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("chart")
public class ChartController {
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private IChartService chartService;

    @RequestMapping("order")
    public String list(@ModelAttribute("qo") OrderChartQueryObject qo, Model model){
        model.addAttribute("brands",brandService.list());
        model.addAttribute("suppliers",supplierService.list());
        model.addAttribute("orderMap",Dictionary.ORDER_MAP);
        model.addAttribute("list",chartService.selectOrderChart(qo));
        return "chart/order";
    }
    @RequestMapping("sale")
    public String sale(@ModelAttribute("qo")SaleChartQueryObject qo, Model model){
        model.addAttribute("brands",brandService.list());
        model.addAttribute("clients",clientService.list());
        model.addAttribute("saleMap",Dictionary.SALE_MAP);
        model.addAttribute("list",chartService.selectSaleChart(qo));
        return "chart/sale";
    }

    @RequestMapping("saleByBar")
    public String saleByBar(@ModelAttribute("qo")SaleChartQueryObject qo, Model model) throws JsonProcessingException {
        //向网页中共享三个数据:分组类型,x坐标,y坐标
        model.addAttribute("groupType",Dictionary.SALE_MAP.get(qo.getGroupType()));
        List<Map<String, Object>> list = chartService.selectSaleChart(qo);
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();
        for (Map<String, Object> map : list) {
            x.add(map.get("groupType"));
            y.add(map.get("totalAmount"));
        }
        model.addAttribute("x", JSONUtil.toJSONString(x));
        model.addAttribute("y", JSONUtil.toJSONString(y));
        return "chart/saleByBar";
    }
}
